package com.czhoujunlv.drug;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.chaychan.library.BottomBarLayout;
import com.czhoujunlv.drug.fragment.BuyFragment;
import com.czhoujunlv.drug.fragment.ClassfcationFragment;
import com.czhoujunlv.drug.fragment.FindFragment;
import com.czhoujunlv.drug.fragment.HeadFragment;
import com.czhoujunlv.drug.fragment.MyFragment;

import java.util.ArrayList;
import java.util.List;


public class HeadPage extends AppCompatActivity {

    ViewPager mViewpager;
    BottomBarLayout bottomBarLayout;
    List<Fragment> pageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head_page);

        mViewpager = findViewById(R.id.viewPager);
        bottomBarLayout = findViewById(R.id.bbl);
        initData();
    }
    private void initData(){
        pageList = new ArrayList<>();
        pageList.add(new HeadFragment());
        pageList.add(new ClassfcationFragment());
        pageList.add(new FindFragment());
        pageList.add(new BuyFragment());
        pageList.add(new MyFragment());

        mViewpager.setAdapter(new fragmentAdapter(getSupportFragmentManager()));

        bottomBarLayout.setViewPager(mViewpager);
        bottomBarLayout.setSmoothScroll(false);
    }


    public class fragmentAdapter extends FragmentPagerAdapter {
        public fragmentAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            Log.i("位置：",String.valueOf(position));
            //return pageList.get(position);
            SharedPreferences sharedPreferences= getSharedPreferences("user", MODE_PRIVATE);
            //EventBus.getDefault().post("fragment send");
            boolean islogin = sharedPreferences.getBoolean("isLogin",false);//如果读不到文件或者当key不存在时 返回false
            if(position == 3){
                if(islogin){
                    return pageList.get(position);
                }else{
                    Intent intent = new Intent(HeadPage.this,MainActivity.class);
                    startActivity(intent);
                }
            }
            return pageList.get(position);

        }

        @Override
        public int getCount() {
            return pageList.size();
        }
    }

}